package com.example.petshopping.ui.message;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.petshopping.ui.main.MainActivity;
import com.example.petshopping.ui.category.CategoryActivity;
import com.example.petshopping.ui.message.adapter.MessageAdapter;
import com.example.petshopping.ui.message.model.Message;
import com.example.petshopping.ui.profile.ProfileActivity;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class MessageActivity extends AppCompatActivity implements MessageAdapter.OnMessageClickListener {

    private static final String TAG = "MessageActivity";
    private RecyclerView recyclerViewMessages;
    private List<Message> messageList;
    private MessageAdapter messageAdapter;
    private View notificationBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            setContentView(R.layout.activity_message);
            initViews();
            loadMessages();
            setupRecyclerView();
            setupBottomNavigation();
        } catch (Exception e) {
            Log.e(TAG, "onCreate发生错误: ", e);
            Toast.makeText(this, "页面加载失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    private void initViews() {
        recyclerViewMessages = findViewById(R.id.recycler_messages);
        notificationBar = findViewById(R.id.notification_bar);
        
        // 设置通知栏的关闭按钮
        findViewById(R.id.btn_close_notification).setOnClickListener(v -> {
            notificationBar.setVisibility(View.GONE);
        });
        
        // 设置开启通知按钮
        View btnOpenNotification = findViewById(R.id.btn_open_notification);
        if (btnOpenNotification != null) {
            btnOpenNotification.setOnClickListener(v -> {
                Toast.makeText(this, "正在开启消息通知...", Toast.LENGTH_SHORT).show();
            });
        }
        
        // 设置参与按钮
        ImageView btnParticipate = findViewById(R.id.btn_participate);
        if (btnParticipate != null) {
            btnParticipate.setOnClickListener(v -> {
                Toast.makeText(this, "您点击了参与按钮！", Toast.LENGTH_SHORT).show();
            });
        }
    }

    private void loadMessages() {
        messageList = new ArrayList<>();
        
        // 平台通知
        Message platformNotice = new Message();
        platformNotice.setId(1);
        platformNotice.setType(Message.TYPE_SYSTEM);
        platformNotice.setIcon(R.drawable.clock);
        platformNotice.setTitle("平台通知");
        platformNotice.setTag("系统");
        platformNotice.setTime("星期三 21:25");
        platformNotice.setContent("【标题标题标题标题标题标题标题...】 您围观的拍品...");
        platformNotice.setBadgeCount(0);
        messageList.add(platformNotice);
        
        // 新品开拍
        Message newProduct = new Message();
        newProduct.setId(2);
        newProduct.setType(Message.TYPE_NEW_PRODUCT);
        newProduct.setTitle("新品开拍");
        newProduct.setTag("系统");
        newProduct.setContent("为你推荐新品好物");
        newProduct.setBadgeCount(0);
        messageList.add(newProduct);
        
        // 水族馆消息1
        Message aquarium1 = new Message();
        aquarium1.setId(3);
        aquarium1.setType(Message.TYPE_NORMAL);
        aquarium1.setIcon(R.drawable.cat);
        aquarium1.setTitle("水族馆");
        aquarium1.setContent("这个是性价比最高的款式了");
        aquarium1.setRightImage(R.drawable.petconnect);
        aquarium1.setBadgeCount(3);
        messageList.add(aquarium1);
        
        // 水族馆消息2
        Message aquarium2 = new Message();
        aquarium2.setId(4);
        aquarium2.setType(Message.TYPE_NORMAL);
        aquarium2.setIcon(R.drawable.cat);
        aquarium2.setTitle("水族馆");
        aquarium2.setContent("这个是性价比最高的款式了");
        aquarium2.setRightImage(R.drawable.petconnect);
        aquarium2.setBadgeCount(3);
        messageList.add(aquarium2);
    }

    private void setupRecyclerView() {
        messageAdapter = new MessageAdapter(this, messageList, this);
        recyclerViewMessages.setLayoutManager(new LinearLayoutManager(this));
        
        // 添加分隔线
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        dividerItemDecoration.setDrawable(ContextCompat.getDrawable(this, R.drawable.bg_message_item_divider));
        recyclerViewMessages.addItemDecoration(dividerItemDecoration);
        
        recyclerViewMessages.setAdapter(messageAdapter);
    }

    private void setupBottomNavigation() {
        BottomNavigationView bottomNav = findViewById(R.id.bottom_nav);
        
        // 设置当前选中项为消息
        bottomNav.setSelectedItemId(R.id.navigation_message);
        
        // 设置点击监听器
        bottomNav.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    startActivity(new Intent(this, MainActivity.class));
                    overridePendingTransition(0,0);
                    finish();
                    return true;
                case R.id.navigation_category:
                    startActivity(new Intent(this, CategoryActivity.class));
                    overridePendingTransition(0,0);
                    finish();
                    return true;
                case R.id.navigation_message:
                    // 当前已在消息页面
                    return true;
                case R.id.navigation_profile:
                    startActivity(new Intent(this, ProfileActivity.class));
                    overridePendingTransition(0,0);
                    finish();
                    return true;
            }
            return false;
        });
        
        // 设置中间按钮点击事件
        findViewById(R.id.center_button).setOnClickListener(v -> {
            Toast.makeText(this, "此功能即将上线", Toast.LENGTH_SHORT).show();
        });
    }
    
    @Override
    public void onMessageClick(Message message) {
        // 点击消息时，跳转到消息详情页
        if (message == null) {
            Log.e(TAG, "点击了空消息对象");
            Toast.makeText(this, "消息数据错误", Toast.LENGTH_SHORT).show();
            return;
        }

        try {
            Log.d(TAG, "点击消息: ID=" + message.getId() + ", 标题=" + message.getTitle());
            
            // 创建意图并添加消息数据
            Intent intent = new Intent(this, MessageDetailActivity.class);
            intent.putExtra("message_id", message.getId());
            intent.putExtra("message_title", message.getTitle());
            intent.putExtra("message_content", message.getContent());
            
            // 使用更安全的方式启动Activity
            startActivity(intent);
            Log.d(TAG, "已发送Intent");
        } catch (Exception e) {
            Log.e(TAG, "打开详情页失败", e);
            Toast.makeText(this, "打开详情页失败，请稍后重试", Toast.LENGTH_SHORT).show();
        }
    }
} 